package commons.entity;

import commons.util.GameUtil;
import fgoScript.constant.GameConstant;
import fgoScript.entity.PointColor;
import fgoScript.exception.AppNeedRestartException;
import fgoScript.exception.AppNeedStopException;

import java.awt.*;
import java.util.Random;

public abstract class absMyMouse implements InMyMouse{
    private static Random ran = new Random(2);

    @Override
    public void mouseMoveByPoint(Point point, boolean delay, boolean Random) throws AppNeedStopException {
        if(GameUtil.STOP_SCRIPT){
            GameUtil.STOP_SCRIPT = false;
            throw new AppNeedStopException();
        }
        if(Random){
            mouseMove((int)point.getX()+1+ran.nextInt(3),
                    (int)point.getY()+1+ran.nextInt(3)
            );
        }else{
            mouseMove((int)point.getX(), (int)point.getY());
        }
        if(delay){
            GameUtil.delay(GameConstant.DELAY);
        }
    }

    @Override
    public void mousePressAndRelease(int delay) {
        mousePress();
        GameUtil.delay(delay);
        mouseRelease();
//        GameUtil.delay(GameConstant.DELAY);
//        GameUtil.delay(GameConstant.DELAY);
    }

    @Override
    public void mousePressAndReleaseForConfirm(PointColor pc) throws AppNeedRestartException {
        Point p;
        Color c;
        boolean isEqual;
        if (pc==null) {
            p = MouseInfo.getPointerInfo().getLocation();
            c = GameUtil.getScreenPixel(p);
            isEqual = true;
        }else {
            p = pc.getPoint();
            c = pc.getColor();
            isEqual = pc.isEqual();
        }
        Color colorBefore;
        Color colorAfter;
        int count =10;
        boolean flag;
        boolean flag2;
        do {
            colorBefore = GameUtil.getScreenPixel(p);
            mousePress();
            GameUtil.delay(GameConstant.DELAY/4);
            mouseRelease();
            GameUtil.delay(GameConstant.DELAY*4);
            colorAfter = GameUtil.getScreenPixel(p);
            flag = GameUtil.likeEqualColor(c, colorAfter);
            flag2 = GameUtil.likeEqualColor(colorBefore, colorAfter);
            if (pc == null) {
                flag = flag || flag2;
            }else {
                if (isEqual) {
                    flag = !flag;
                }
            }
            if (count-- < 0) {
                throw new AppNeedRestartException();
            }
        } while (flag);

    }

    @Override
    public void moveBySteps(Point start, Point end) {
        int actor = end.getX() + end.getY() - start.getX() - start.getY() > 0 ? 1 : -1;
        if ((int) start.getX() == (int) end.getX()) {

            for (int i = (int) start.getY(); (i - end.getY()) * actor < 0; i += GameConstant.MOVE_STEP * actor) {
                mouseMove((int) start.getX(), i);
                if (i == (int) start.getY()) {
                    GameUtil.delay(GameConstant.DELAY);
                    mousePress();
                }
                GameUtil.delay(GameConstant.MOVE_TIME);
            }
        } else {
            for (int i = (int) start.getX(); (i - end.getX()) * actor < 0; i += GameConstant.MOVE_STEP * actor) {
                mouseMove(i, (int) start.getY());
                if (i == (int) start.getX()) {
                    GameUtil.delay(GameConstant.DELAY);
                    mousePress();
                }
                GameUtil.delay(GameConstant.MOVE_TIME);
            }
        }
        mouseMove((int) end.getX(), (int) end.getY());
        GameUtil.delay(GameConstant.DELAY);
        mouseRelease();
    }
}
